package net.flintx.array;

/**
 * Created by flintx on 2018/1/10.
 */
public class ReshapeTheMatrix {
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        if (nums.length == 0 || nums[0].length == 0)
            return nums;
        int matrixSize = nums.length * nums[0].length;
        if (r * c != matrixSize)
            return nums;
        int[][] res = new int[r][c];
        int count = 0;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                res[i][j] = nums[count / nums[0].length][count % nums[0].length];
                count += 1;
            }
        }
        return res;
    }
}
